package com.getpebble.android.main.sections.developer;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.TextView;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.framework.fragment.PblBaseFragment;
import com.getpebble.android.common.model.FrameworkState;
import com.getpebble.android.common.model.FrameworkState.EventType;
import com.getpebble.android.framework.FrameworkEventReceiver;
import com.getpebble.android.framework.FrameworkEventReceiver.IFrameworkEventListener;
import com.getpebble.android.framework.PblFrameworkInterface;

public class DeveloperFragment extends PblBaseFragment implements IFrameworkEventListener {
    private static final String TAG = DeveloperFragment.class.getSimpleName();
    private TextView mConnectionMgrStatus;
    private TextView mConnectionStatus;
    private CheckedTextView mEnabledCheck;
    private PblFrameworkInterface mFrameworkInterface;
    private TextView mProxyStatus;
    private TextView mServerAddress;

    public int getLayoutId() {
        return R.layout.fragment_developer;
    }

    public void init(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.mFrameworkInterface = PebbleApplication.getFrameworkInterface();
        this.mConnectionStatus = (TextView) container.findViewById(R.id.connection_status);
        this.mServerAddress = (TextView) container.findViewById(R.id.server_ip_addr);
        this.mEnabledCheck = (CheckedTextView) container.findViewById(R.id.connection_enabled_tv);
        this.mConnectionMgrStatus = (TextView) container.findViewById(R.id.conn_mgr_status);
        this.mProxyStatus = (TextView) container.findViewById(R.id.proxy_status);
        Trace.debug(TAG, "init()");
        this.mEnabledCheck.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                DeveloperFragment.this.mEnabledCheck.toggle();
                if (DeveloperFragment.this.mEnabledCheck.isChecked()) {
                    Trace.debug(DeveloperFragment.TAG, "User clicked start developer connection");
                    DeveloperFragment.this.mFrameworkInterface.startDeveloperConnection();
                    return;
                }
                Trace.debug(DeveloperFragment.TAG, "User clicked stop developer connection");
                DeveloperFragment.this.mFrameworkInterface.stopDeveloperConnection();
            }
        });
    }

    public void onResume() {
        super.onResume();
        Trace.debug(TAG, "onResume()");
        FrameworkEventReceiver.registerFrameworkStateEventListener(this);
        updateUI(FrameworkEventReceiver.getLatestFrameworkState());
        if (this.mFrameworkInterface != null) {
            this.mFrameworkInterface.pingDeveloperConnectionProxy();
        }
    }

    public void onPause() {
        Trace.debug(TAG, "onPause()");
        FrameworkEventReceiver.unregisterFrameworkStateEventListener(this);
        super.onPause();
    }

    public void onFrameworkStateChanged(FrameworkState newState) {
        if (newState == null) {
            Trace.debug(TAG, "onFrameworkStateChanged() state is null");
            return;
        }
        EventType lastEvent = newState.getLastEvent();
        if (lastEvent == null) {
            Trace.error(TAG, "onFrameworkStateChanged: Last event was null. Not updating UI");
        } else if (lastEvent.equals(EventType.DEVELOPER_CONNECTION_CHANGED)) {
            updateUI(newState);
        }
    }

    private void updateUI(FrameworkState frameworkState) {
        Trace.debug(TAG, "updateUI()");
        if (frameworkState == null) {
            Trace.warning(TAG, "updateUI(): frameworkState is null.");
            return;
        }
        if (frameworkState.getDeveloperConnectionIpAddress() != null) {
            this.mServerAddress.setText(frameworkState.getDeveloperConnectionIpAddress());
            this.mConnectionStatus.setText(String.format(getString(R.string.developer_listening), new Object[]{frameworkState.getDeveloperConnectionIpAddress()}));
            this.mEnabledCheck.setChecked(true);
        } else {
            this.mConnectionStatus.setText(R.string.developer_server_stopped);
            this.mEnabledCheck.setChecked(false);
        }
        if (frameworkState.isDeveloperConnectionConnected()) {
            this.mConnectionMgrStatus.setText(R.string.developer_connected);
        } else {
            this.mConnectionMgrStatus.setText(R.string.developer_disconnected);
        }
        if (frameworkState.isDeveloperConnectionProxyConnected()) {
            this.mProxyStatus.setText(R.string.developer_connected);
        } else {
            this.mProxyStatus.setText(R.string.developer_disconnected);
        }
    }
}
